package com.example.experiment3

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.widget.Button
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class MainActivity4 : AppCompatActivity() {

    private lateinit var textView: TextView
    private lateinit var textView2: TextView
    private lateinit var button: Button
    private lateinit var button2: Button
    private lateinit var button3: Button
    private lateinit var button4: Button
    
    private var second = 0
    private var running = false
    private var count = 0
    private val handler = Handler(Looper.getMainLooper())
    private var runnable: Runnable? = null
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main4)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        // 初始化视图
        initViews()

        // 恢复状态
        if (savedInstanceState != null) {
            second = savedInstanceState.getInt("second", 0)
            running = savedInstanceState.getBoolean("running", false)
            count = savedInstanceState.getInt("count", 0)
        }
        
        // 设置点击监听器
        setupClickListeners()
        
        // 开始计时器
        startTimer()
    }
    
    private fun initViews() {
        textView = findViewById(R.id.textView)
        textView2 = findViewById(R.id.textView2)
        button = findViewById(R.id.button)
        button2 = findViewById(R.id.button2)
        button3 = findViewById(R.id.button3)
        button4 = findViewById(R.id.button4)
    }
    
    private fun setupClickListeners() {
        button.setOnClickListener {
            running = true
            button.isEnabled = false
            button2.isEnabled = true
        }
        
        button2.setOnClickListener {
            running = false
            button.isEnabled = true
            button2.isEnabled = false
        }
        
        button3.setOnClickListener {
            running = false
            second = 0
            count = 0
            button.isEnabled = true
            button2.isEnabled = false
            updateDisplay()
        }
        
        button4.setOnClickListener {
            finish()
        }
    }
    
    private fun startTimer() {
        runnable = object : Runnable {
            override fun run() {
                val hou = second / 3600
                val min = (second % 3600) / 60
                val sec = second % 60
                textView.text = String.format("%02d:%02d:%02d", hou, min, sec)
                textView2.text = getString(R.string.count_display, count)
                
                if (running) {
                    second++
                    count++
                }
                handler.postDelayed(this, 1000)
            }
        }
        handler.post(runnable!!)
    }
    
    private fun updateDisplay() {
        val hou = second / 3600
        val min = (second % 3600) / 60
        val sec = second % 60
        textView.text = String.format("%02d:%02d:%02d", hou, min, sec)
        textView2.text = getString(R.string.count_display, count)
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putInt("second", second)
        outState.putBoolean("running", running)
        outState.putInt("count", count)
    }
    
    override fun onDestroy() {
        super.onDestroy()
        handler.removeCallbacks(runnable!!)
    }
}